package com.mask.eurekaconsumer;

import com.netflix.discovery.EurekaClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@Slf4j
public class MainController3 {

	@Autowired
	// 抽象
	DiscoveryClient client;
	
	@Autowired
	EurekaClient client2;
	
	@Autowired
	LoadBalancerClient lb;

	@Autowired
	RestTemplate restTemplate;

	/**
     * 自动处理一个url
	 * @return
     */
	@GetMapping("/client10")
	@ResponseBody
	public Object client10() {

//		ServiceInstance instance = lb.choose("eureka-provider");

		String url ="http://eureka-provider/getHi";


		String respStr = restTemplate.getForObject(url, String.class);
		ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
		log.info("getForEntity:{}",entity);

		log.info("返回结果client10:{}",respStr);

		return respStr;
	}


	@GetMapping("/client11")
	@ResponseBody
	public Object client11() {

		String url ="http://eureka-provider/getMap";


		Map map = restTemplate.getForObject(url, Map.class);
		log.info("getForObject:{}",map);

		return map;
	}

	@GetMapping("/client12")
	@ResponseBody
	public Object client12() {

		String url ="http://eureka-provider/getObj";


		Person person = restTemplate.getForObject(url, Person.class);
		log.info("getForObject:{}",person);

		return person;
	}


	@GetMapping("/client13")
	@ResponseBody
	public Object client13() {

		String url ="http://eureka-provider/getObj2?name={1}";


		Person person = restTemplate.getForObject(url, Person.class,"mask-hx");
		log.info("getForObject:{}",person);

		return person;
	}

	@GetMapping("/client14")
	@ResponseBody
	public Object client14() {

		String url ="http://eureka-provider/getObj2?name={name}";
		Map<String, String> map = Collections.singletonMap("name", "mask-SingletonMap");

		Person person = restTemplate.getForObject(url, Person.class,map);
		log.info("getForObject:{}",person);

		return person;
	}

	@SneakyThrows
	@GetMapping("/client15")
	@ResponseBody
	public Object client15(HttpServletResponse response) {

		String url ="http://eureka-provider/postLocation";
		Map<String, String> map = Collections.singletonMap("name", "location");

		URI uri = restTemplate.postForLocation(url, map, Person.class);

		response.sendRedirect(uri.toURL().toString());
		log.info("getForObject:{}",uri);

		return uri;
	}
}
